package com.tonysong.oa.yeboa12.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tonysong.oa.yeboa12.config.domain.Login;
import com.tonysong.oa.yeboa12.config.util.R;
import com.tonysong.oa.yeboa12.pojo.Admin;
import com.tonysong.oa.yeboa12.service.AdminService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tonysong
 * @since 2022-12-01
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;

    @PostMapping("/login")
    @ApiOperation(value = "登录并返回token",notes = "post请求")
    public R login(@RequestBody Login login){
       return this.adminService.login(login);
    }

    @GetMapping("/logout")
    @ApiOperation("注销操作")
    public  R logout(){
        return new R(200,true,"注销成功！");
    }

    @GetMapping("/info")
    @ApiOperation("用户信息")
    public R getAdminInfo(java.security.Principal principal){
        String name = principal.getName();
        Admin admin = this.adminService.getOne(new LambdaQueryWrapper<Admin>().eq(Admin::getUsername, name));
        //防止密码的泄露
        admin.setPassword(null);
        admin.setRoles(this.adminService.findRoles(admin.getId()));
        return new R(200, true, admin);


    }
}
